//
//  ZuoJRefreshControl.swift
//  Tanenbaum_blog
//
//  Created by Tanenbaum on 15/8/4.
//  Copyright © 2015年 GZJ. All rights reserved.
//

import UIKit
private let kRefreshPullOffSet: CGFloat = -60
class ZuoJRefreshControl: UIRefreshControl {

    override func endRefreshing() {
        
        super.endRefreshing()
         refreshView.stopLoading()
    }
    
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        
        if frame.origin.y > 0 {
            return
        }
        
        if refreshing {
            
           refreshView.startLoading()
        }
      
        if frame.origin.y < kRefreshPullOffSet && !refreshView.rotateFlag {
            print("箭头朝下")
            refreshView.rotateFlag = true
        }else if frame.origin.y > kRefreshPullOffSet && refreshView.rotateFlag {
            print("箭头朝上")
            refreshView.rotateFlag = false
        }

    }
   override init() {
        super.init()
        setupUI()
    }

   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
    deinit {
        self.removeObserver(self, forKeyPath: "frame")
    }
    
    private func setupUI() {
        
        addObserver(self, forKeyPath: "frame", options: NSKeyValueObservingOptions(rawValue: 0), context: nil)
        tintColor = UIColor.clearColor()
        addSubview(refreshView)
        
        refreshView.ff_AlignInner(type: ff_AlignType.CenterCenter, referView: self, size: refreshView.bounds.size)
        
    }
    
    //MARK --懒加载
    private lazy var refreshView: ZuoJRefreshView = ZuoJRefreshView.refreshView()
}

class ZuoJRefreshView: UIView {
    
    private var rotateFlag = false {
        didSet {
            
            rotateIcon()
        }
    }
    
    @IBOutlet weak var loadingIcon: UIImageView!
    @IBOutlet weak var tipView: UIView!
    @IBOutlet weak var tipIcon: UIImageView!
    
    class func refreshView() ->ZuoJRefreshView {
        
        return NSBundle.mainBundle().loadNibNamed("ZuoJRefresh", owner: nil, options: nil).last as! ZuoJRefreshView
    }
    private func rotateIcon() {
        
        let angle = rotateFlag ? CGFloat(M_PI - 0.01) : CGFloat(M_PI + 0.01)
        UIView.animateWithDuration(0.25) { () -> Void in
            self.tipIcon.transform = CGAffineTransformRotate(self.tipIcon.transform, angle)
        }
    }
    
    private func startLoading() {
        
        if loadingIcon.layer.animationForKey("loadingAnim") != nil {
            return
        }
        
          tipView.hidden = true
        
          let anim = CABasicAnimation(keyPath: "transform.rotation")
        
          anim.toValue = 2 * M_PI
          anim.duration = 1
          anim.repeatCount = MAXFLOAT
        
          loadingIcon.layer.addAnimation(anim, forKey: "loadingAnim")
        
    }
    private func stopLoading() {
        
        tipView.hidden = false
        loadingIcon.layer.removeAllAnimations()
    }
}